package top.mschen.blog.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;
import top.mschen.blog.service.AsyncT;

import java.util.concurrent.Future;

/**
 * 测试一步任务
 */
@Service
public class AsyncTestImpl implements AsyncT {

    private static final Logger log = LoggerFactory.getLogger(AsyncTestImpl.class);

    /**
     * 最简单的异步调用，返回值为void
     */
    @Async("taskExecutor")
    public void  AsynInvokeSimple(){

        log.info( " 无参数无返回值的最简单的异步任务");
    }
    /**
     * 带参数的异步调用 异步方法可以传入参数
     */
    @Async("taskExecutor")
    public void AsynInvokeParam(String param){
//        int i= 1/0;
        log.info( " 带参数的异步调用 异步方法可以传入参: {}",param);
    }

    @Async
   public Future AsynInvokeReturn( int i) {
       log.info("asyncInvokeReturnFuture, param= {}", i);
       Future<String> future;
       try {
           Thread.sleep(1000 * i);
           future = new AsyncResult<String>("success:" + i);
       } catch (InterruptedException e) {
           future = new AsyncResult<String>("error");
       }
       return future;
   }
}